package com.test.mooan.labama.activity_fragment_boom.fragment_boom;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.viewpager2.widget.ViewPager2;

import com.test.mooan.labama.R;
import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseLnFragment_boom;
import com.test.mooan.labama.activity_fragment_boom.ln_boom.LnMainActivity_boom;
import com.test.mooan.labama.adapter_boom.ProductListAdapter;
import com.test.mooan.labama.bean_boom.product.ProductListInfo;
import com.test.mooan.labama.constants_boom.Constant_boom;
import com.test.mooan.labama.dialog_boom.CustomerServiceDialog_boom;
import com.test.mooan.labama.utils_boom.SizeUtils;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;
import com.test.mooan.labama.utils_boom.http_boom.HttpManager_boom;


public class LnMainFragmentForOldUser_boom extends BaseLnFragment_boom {

    private ImageView hotListIv;
    private TextView hotListTv, repayTv;
    private View hotListLine, repayLine;
    private ViewPager2 viewPager;

    private ProductListAdapter adapter;

    private int showTab = 0;

    private ViewPager2.OnPageChangeCallback onPageChangeCallback;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageTag = "HOME";
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ln_main_for_old_user_boom, container, false);
        init_boom(view);
        return view;
    }

    @SuppressLint("SetTextI18n")
    private void init_boom(View view) {

        view.findViewById(R.id.home_ib_customer_service).setOnClickListener(v -> {
            new CustomerServiceDialog_boom(requireContext()).show();
        });

        hotListIv = view.findViewById(R.id.home_iv_hot_list);
        hotListTv = view.findViewById(R.id.home_tv_hot_list);
        repayTv = view.findViewById(R.id.home_tv_repay);
        hotListLine = view.findViewById(R.id.home_view_line_hot_list);
        repayLine = view.findViewById(R.id.home_view_line_repay);
        viewPager = view.findViewById(R.id.home_view_pager);

        adapter = new ProductListAdapter();
        viewPager.setAdapter(adapter);

        view.findViewById(R.id.home_view_tab_hot_list).setOnClickListener(v -> {
            if (showTab == 0) return;
            UserInfoHelper_boom.gInstance().addEventPoint("HOME_TAB_HOTLIST_CLICK");
            viewPager.setCurrentItem(0);
        });

        view.findViewById(R.id.home_view_tab_repay).setOnClickListener(v -> {
            if (showTab == 1) return;
            UserInfoHelper_boom.gInstance().addEventPoint("HOME_TAB_REPAY_CLICK");
            viewPager.setCurrentItem(1);
        });

        onPageChangeCallback = new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                if (position==0) tabChangeToHotList();
                else tabChangeToRepay();
            }
        };

        viewPager.registerOnPageChangeCallback(onPageChangeCallback);

        SwipeRefreshLayout refreshLayout = view.findViewById(R.id.home_refresh_layout);
        refreshLayout.setOnRefreshListener(() -> {
            refreshLayout.setRefreshing(false);
            HttpManager_boom.getInstance().requestUserProcess();
        });

        HttpManager_boom.getInstance().queryProducts();

    }

    @Override
    public void onResume() {
        super.onResume();

        if (LnMainActivity_boom.toRepayListFlag) {
            LnMainActivity_boom.toRepayListFlag = false;
            showRepayView();
        }

    }

    private void tabChangeToHotList(){
        try {
            showTab = 0;
            ViewGroup.LayoutParams layoutParams = hotListIv.getLayoutParams();
            int sizePx = SizeUtils.dpToPx(20);
            layoutParams.width = sizePx;
            layoutParams.height = sizePx;
            hotListIv.setLayoutParams(layoutParams);

            hotListTv.setTextColor(Color.BLACK);
            hotListTv.setTypeface(Typeface.create("sans-serif-black", Typeface.NORMAL));
            hotListTv.setTextSize(20);

            hotListLine.setVisibility(View.VISIBLE);


            repayTv.setTextColor(Color.parseColor("#FF959CA8"));
            repayTv.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL));
            repayTv.setTextSize(16);

            repayLine.setVisibility(View.INVISIBLE);

        }catch (Exception e){
            if (Constant_boom.Debug) e.printStackTrace();
        }
    }

    private void tabChangeToRepay(){
        try {
            showTab = 1;
            ViewGroup.LayoutParams layoutParams = hotListIv.getLayoutParams();
            int sizePx = SizeUtils.dpToPx(14);
            layoutParams.width = sizePx;
            layoutParams.height = sizePx;
            hotListIv.setLayoutParams(layoutParams);

            hotListTv.setTextColor(Color.parseColor("#FF959CA8"));
            hotListTv.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL));
            hotListTv.setTextSize(16);

            hotListLine.setVisibility(View.INVISIBLE);


            repayTv.setTextColor(Color.BLACK);
            repayTv.setTypeface(Typeface.create("sans-serif-black", Typeface.NORMAL));
            repayTv.setTextSize(20);

            repayLine.setVisibility(View.VISIBLE);

        }catch (Exception e){
            if (Constant_boom.Debug) e.printStackTrace();
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    public void onListChange(){
        runOnAttach(() -> {
            adapter.notifyDataSetChanged();
            if (ProductListInfo.gtInstance().gtProductList().isEmpty() && !ProductListInfo.gtInstance().gtRepayProductList().isEmpty())
                showRepayView();
            HttpManager_boom.getInstance().fetchDropConfig();
        });
    }

    public void showRepayView(){
        runOnAttach(() -> {
            if (viewPager==null) return;
            viewPager.setCurrentItem(1);
        });
    }

    @Override
    public void onDestroy() {
        if (onPageChangeCallback!=null) viewPager.unregisterOnPageChangeCallback(onPageChangeCallback);
        super.onDestroy();
    }
}